#!/bin/bash

clear


echo 适用于SteamOS和Bazzite的Clover双引导安装脚本
echo 咸鱼:SteamOS超人 汉化
echo 进行初步的健全性检查 ...
sleep 2

# check if running on Steam Deck OLED or LCD
if [ "$(cat /sys/class/dmi/id/board_name)" = "Jupiter" ] || [ "$(cat /sys/class/dmi/id/board_name)" = "Galileo" ] 
then
	echo 脚本正在支持的型号上运行-Steam Deck $(cat /sys/class/dmi/id/board_name).

# check if running on Lenovo Legion GO S
elif [ "$(cat /sys/class/dmi/id/product_name)" = "83N6" ] || [ "$(cat /sys/class/dmi/id/product_name)" = "83L3" ] || [ "$(cat /sys/class/dmi/id/product_name)" = "83Q2" ] || [ "$(cat /sys/class/dmi/id/product_name)" = "83Q3" ]
then
	echo 脚本正在支持的型号上运行 - Legion Go S $(cat /sys/class/dmi/id/product_name).
	echo 为Legion GO S 创建特定配置
	sed -i '/<key>Enabled<\/key>/!b;n;c\\t\t\t<true\/>' custom/config.plist
	sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>1920x1200<\/string>' custom/config.plist

# check if running on Lenovo Legion GO
elif [ "$(cat /sys/class/dmi/id/product_name)" = "83E1" ]
then
	echo 脚本正在支持的型号上运行 - Legion Go $(cat /sys/class/dmi/id/product_name).
	echo 为Legion GO 创建特定配置
	sed -i '/<key>Enabled<\/key>/!b;n;c\\t\t\t<true\/>' custom/config.plist
	sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>2560x1600<\/string>' custom/config.plist

# check if running on Asus ROG Ally
elif [ "$(cat /sys/class/dmi/id/board_name)" = "RC71L" ]
then
	echo 脚本正在支持的型号上运行 - Asus ROG Ally $(cat /sys/class/dmi/id/board_name).
	echo 为Asus ROG Ally 创建特定配置
	sed -i '/<key>Enabled<\/key>/!b;n;c\\t\t\t<true\/>' custom/config.plist
	sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>1920x1080<\/string>' custom/config.plist

# check if running on Asus ROG Ally X
elif [ "$(cat /sys/class/dmi/id/board_name)" = "RC72LA" ]
then
	echo 脚本正在支持的型号上运行- Asus ROG Ally X $(cat /sys/class/dmi/id/board_name).
	echo 为Asus ROG Ally X 创建特定配置
	sed -i '/<key>Enabled<\/key>/!b;n;c\\t\t\t<true\/>' custom/config.plist
	sed -i '/<key>ScreenResolution<\/key>/!b;n;c\\t\t<string>1920x1080<\/string>' custom/config.plist
else
	echo 不支持的设备！立即退出.
	exit
fi


# check if  dual boot configuration is supported
blkid | grep nvme0n1p1\: | grep Microsoft
if [ $? -eq 0 ]
then
	echo 检测到不支持的双引导配置.
	echo 确保在Windows之前安装了 $OS!
	exit
else
	echo 支持双引导配置.
fi

# define variables here
CLOVER=$(efibootmgr | grep -i Clover | colrm 9 | colrm 1 4)
REFIND=$(efibootmgr | grep -i rEFInd | colrm 9 | colrm 1 4)
ESP=$(df /dev/nvme0n1p1 --output=avail | tail -n1)
CLOVER_VERSION=5163
CLOVER_URL=https://gitee.com/songy171/SteamDeck-Clover-dualboot/releases/download/$CLOVER_VERSION/Clover-$CLOVER_VERSION-X64.iso.7z
CLOVER_ARCHIVE=$(curl -s -O -L -w "%{filename_effective}" $CLOVER_URL)
CLOVER_BASE=$(basename -s .7z $CLOVER_ARCHIVE)
CLOVER_EFI=\\EFI\\clover\\cloverx64.efi


# sanity check - is there enough space on esp
mkdir ~/temp-ESP
echo -e "$current_password\n" | sudo -S mount /dev/nvme0n1p1 ~/temp-ESP
if [ $? -eq 0 ]
then
	echo ESP分区已挂载.
else
	echo 错误挂载ESP分区.
	rmdir ~/temp-ESP
	exit
fi

if [ $ESP -ge 15000 ]
then
	echo ESP分区有$ESP KB可用空间.
	echo ESP分区有足够的可用空间.
	echo -e "$current_password\n" | sudo -S umount ~/temp-ESP
	rmdir ~/temp-ESP
else
	echo ESP分区有$ESP KB可用空间.
	echo ESP分区空间不足!
 	echo -e "$current_password\n" | sudo -S du -hd2 /esp
	echo -e "$current_password\n" | sudo -S umount ~/temp-ESP
	rmdir ~/temp-ESP
	exit
fi

# sanity check - is rEFInd installed?
efibootmgr | grep -i refind
if [ $? -ne 0 ]
then
	echo 未检测到rEFInd！继续安装四叶草.
else
	echo rEFInd似乎已安装！尽最大努力卸载rEFInd!
	for rEFInd_boot in $REFIND
	do
		echo -e "$current_password\n" | sudo -S efibootmgr -b $rEFInd_boot -B &> /dev/null
	done
	echo -e "$current_password\n" | sudo -S systemctl disable bootnext-refind.service &> /dev/null
	echo -e "$current_password\n" | sudo -S systemctl disable rEFInd_bg_randomizer.service
	echo -e "$current_password\n" | sudo -S rm -rf /run/media/gamer/esp/EFI/refind &> /dev/null
	echo -e "$current_password\n" | sudo -S steamos-readonly disable
	echo -e "$current_password\n" | sudo -S rm /etc/systemd/system/bootnext-refind.service &> /dev/null
	echo -e "$current_password\n" | sudo -S rm -f /etc/systemd/system/rEFInd_bg_randomizer.service
	echo -e "$current_password\n" | sudo -S pacman-key --init
	echo -e "$current_password\n" | sudo -S pacman-key --populate archlinux
	echo -e "$current_password\n" | sudo -S pacman -R --noconfirm SteamDeck_rEFInd
	echo -e "$current_password\n" | sudo -S steamos-readonly enable
	rm -fr ~/.local/SteamDeck_rEFInd
	rm -rf ~/.SteamDeck_rEFInd &> /dev/null
	rm -f ~/Desktop/SteamDeck_rEFInd.desktop

	# check again if rEFInd is gone?
	efibootmgr | grep -i refind
	if [ $? -ne 0 ]
	then
		echo rEFInd已成功卸载！继续安装四叶草.
	else
		echo rEFInd仍存在中。请先手动卸载rEFInd!
		exit
	fi
fi

# obtain Clover ISO
7z x $CLOVER_ARCHIVE -aoa $CLOVER_BASE &> /dev/null
if [ $? -eq 0 ]
then
	echo 四叶草已从gitee仓库下载!
else
	echo 下载四叶草时出错!
	exit
fi

# mount Clover ISO
mkdir ~/temp-clover &> /dev/null
echo -e "$current_password\n" | sudo -S mount $CLOVER_BASE ~/temp-clover &> /dev/null
if [ $? -eq 0 ]
then
	echo 四叶草镜像已安装!
else
	echo 安装ISO时出错!
	echo -e "$current_password\n" | sudo -S umount ~/temp-clover
	rmdir ~/temp-clover
	exit
fi

# copy Clover files to EFI system partition
echo -e "$current_password\n" | sudo -S cp -Rf ~/temp-clover/efi/clover /run/media/gamer/esp/EFI
echo -e "$current_password\n" | sudo -S cp custom/config.plist /run/media/gamer/esp/EFI/clover/config.plist
echo -e "$current_password\n" | sudo -S cp -Rf custom/themes/* /run/media/gamer/esp/EFI/clover/themes

# delete temp directories created and delete the Clover ISO
echo -e "$current_password\n" | sudo -S umount ~/temp-clover
rmdir ~/temp-clover
rm Clover-$CLOVER_VERSION-X64.iso*

# remove previous Clover entries before re-creating them
for entry in $CLOVER
do
	echo -e "$current_password\n" | sudo -S efibootmgr -b $entry -B &> /dev/null
done

# install Clover to the EFI system partition
echo -e "$current_password\n" | sudo -S efibootmgr -c -d /dev/nvme0n1 -p 1 -L "Clover - GUI Boot Manager" -l "$CLOVER_EFI" &> /dev/null

# check if bootx64.efi.orig already exists
echo -e "$current_password\n" | sudo -S test -e /run/media/gamer/esp/EFI/Boot/.orig
if [ $? -eq 0 ]
then
	echo /run/media/gamer/esp/EFI/Boot/.orig 已找到-无需任何操作.
else
	echo /run/media/gamer/esp/EFI/Boot/ 备份未发现.
	echo -e "$current_password\n" | sudo -S cp /run/media/gamer/esp/EFI/Boot/ bootx64.orig
	echo -e "$current_password\n" | sudo -S cp /run/media/gamer/esp/EFI/clover/cloverx64.efi /run/media/gamer/esp/EFI/Boot/
	echo 复制四叶草 EFI 到 /run/media/gamer/esp/EFI/Boot/ - 已完成.
fi

# check if Windows EFI needs to be disabled!
echo -e "$current_password\n" | sudo -S test -e /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi.orig
if [ $? -eq 0 ]
then
	echo Windows EFI备份已存在。检查是否已禁用Windows EFI.
	echo -e "$current_password\n" | sudo -S test -e /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi
	if [ $? -eq 0 ]
	then
		echo -e "$current_password\n" | sudo -S mv /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi /run/media/gamer/esp/EFI/Microsoft/bootmgfw.efi &> /dev/null
		echo 需要禁用Windows EFI-已完成.
	else
		echo Windows EFI已禁用-无需任何操作.
	fi
else
	echo Windows EFI备份不存在.
	echo -e "$current_password\n" | sudo -S cp /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi.orig &> /dev/null
	echo -e "$current_password\n" | sudo -S mv /run/media/gamer/esp/EFI/Microsoft/Boot/bootmgfw.efi /run/media/gamer/esp/EFI/Microsoft/bootmgfw.efi &> /dev/null
	echo 需要禁用Windows EFI-已完成.
fi

# re-arrange the boot order and make Clover the priority!
echo -e "$current_password\n" | sudo -S efibootmgr -n $CLOVER &> /dev/null
echo -e "$current_password\n" | sudo -S efibootmgr -o $CLOVER &> /dev/null

# Final sanity check
efibootmgr | grep "Clover - GUI" &> /dev/null
if [ $? -eq 0 ]
then
	echo 四叶草已成功安装到EFI系统分区!
else
	echo 哇，出了点问题。四叶草未安装.
	exit
fi

# create ~/1Clover-tools and place the scripts in there
mkdir ~/1Clover-tools &> /dev/null
rm -f ~/1Clover-tools/* &> /dev/null
cp custom/Clover-Toolbox.sh ~/1Clover-tools &> /dev/null
echo -e "$current_password\n" | sudo -S cp custom/clover-bootmanager.service custom/clover-bootmanager.sh /etc/systemd/system
cp -R custom/logos ~/1Clover-tools &> /dev/null
cp -R custom/efi ~/1Clover-tools &> /dev/null

# make the scripts executable
chmod +x ~/1Clover-tools/Clover-Toolbox.sh
echo -e "$current_password\n" | sudo -S chmod +x /etc/systemd/system/clover-bootmanager.sh

# start the clover-bootmanager.service
echo -e "$current_password\n" | sudo -S systemctl daemon-reload
echo -e "$current_password\n" | sudo -S systemctl enable --now clover-bootmanager.service
echo -e "$current_password\n" | sudo -S /etc/systemd/system/clover-bootmanager.sh

# custom config if using SteamOS or Bazzite
if [ $OS = SkorionOS ]
then
	echo 为$OS 进行最终配置.
	mkdir -p ~/.local/share/kservices5/ServiceMenus
	cp custom/open_as_root.desktop ~/.local/share/kservices5/ServiceMenus
	echo -e "$current_password\n" | sudo -S cp custom/clover-whitelist.conf /etc/atomic-update.conf.d
else
	echo 为$OS 进行最终配置.
	echo -e "$current_password\n" | sudo -S blkid | grep nvme0n1p1 | grep esp &> /dev/null
	if [ $? -eq 0 ]
	then
		echo ESP分区已标记-无需任何操作.
	else
		echo -e "$current_password\n" | sudo -S fatlabel /dev/nvme0n1p1 esp &> /dev/null
		echo ESP分区标记已完成.
	fi

	# set bazzite as the default boot in Clover config
	echo -e "$current_password\n" | sudo -S sed -i '/<key>DefaultLoader<\/key>/!b;n;c\\t\t<string>\\efi\\fedora\\shimx64\.efi<\/string>' /run/media/gamer/esp/EFI/clover/config.plist

fi

# create desktop icon for Clover Toolbox
ln -s ~/1Clover-tools/Clover-Toolbox.sh ~/Desktop/双系统引导工具箱 &> /dev/null
echo -e 工具箱桌面图标已创建!

echo 双系统引导安装成功!
